﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;

public partial class NewEntryWorkstation : EmployeePage
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCredentials();
        _imgConfirm.Visible = IsLoggedIn;
		if (!IsPostBack)
		{
			if (Convert.ToBoolean(Request.QueryString["Change"]))
			{
				var workastation = Facade.Employee().GetWorkstationInstance(
					GetCompanyIdFromCookie(),
					Convert.ToInt32(Request.QueryString["WorkstationId"]));
				_txtWorkstationName.Text = workastation.Name;
			}
		}
    }

    protected void _btnNewWorkstation_Click(object sender, EventArgs e)
    {
	    var workstation = WorkstationEntity.Create(GetCompanyIdFromCookie(),
		    _txtWorkstationName.Text);

	    if (Convert.ToBoolean(Request.QueryString["Change"]))
	    {
		    workstation.WorkstationId = Convert.ToInt32(Request.QueryString["WorkstationId"]);
            try
            {
                Facade.Employee().UpdateWorkstation(workstation);
            }
            catch (SubmitException)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að uppfæra.');", true);
                return;
            }
	    }
	    else
	    {
            try
            {
                Facade.Employee().CreateWorkstation(workstation);
            }
            catch (SubmitException)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að stofna.');", true);
                return;
            }
	    }
	    Server.Transfer("~/Config.aspx",true);
    }
    #endregion

    #region Link Transfer
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx",true);
        }
	}

    protected void _lbtnConfig_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Config.aspx", true);
    }

	protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx", true);
	}
	protected void _lbtnConfig1_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/Config.aspx", true);
	}
	protected void _lbtnCompanyBooking1_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx", true);
	}
    #endregion
}
